package org.luckyjourney.service.residentconsultant.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.luckyjourney.config.QiNiuConfig;
import org.luckyjourney.entity.File;
import org.luckyjourney.entity.ResidentConsultant;
import org.luckyjourney.entity.booking.NavigationBar;
import org.luckyjourney.entity.dto.ResidentConsultantDTO;
import org.luckyjourney.entity.user.User;
import org.luckyjourney.entity.vo.ResidentConsultantVO;
import org.luckyjourney.exception.BaseException;
import org.luckyjourney.mapper.navigationBar.NavigationBarMapper;
import org.luckyjourney.mapper.residentconsultant.ResidentConsultantMapper;
import org.luckyjourney.mapper.user.UserMapper;
import org.luckyjourney.service.FileService;
import org.luckyjourney.service.residentconsultant.ResidentConsultantService;
import org.luckyjourney.util.BaseContext;
import org.luckyjourney.util.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.ObjectName;
import java.time.LocalDateTime;

@Service
public class ResidentConsultantServiceImpl extends ServiceImpl<ResidentConsultantMapper, ResidentConsultant> implements ResidentConsultantService {
    @Autowired
    ResidentConsultantMapper residentConsultantMapper;

    @Autowired
    private FileService fileService;
    @Autowired
    UserMapper userMapper;

    @Override
    public String insert(ResidentConsultant residentConsultant) {
        Integer sex = residentConsultant.getSex();
        Integer certificate = residentConsultant.getCertificate();
        Integer front = residentConsultant.getFront();
        String fullAddress = residentConsultant.getFullAddress();
        if (ObjectUtil.isEmpty(certificate)) {
            throw new BaseException("证书不能为空");
        }
        if (ObjectUtil.isEmpty(front)) {
            throw new BaseException("身份证正面不能为空");
        }
        if (ObjectUtil.isEmpty(fullAddress)) {
            throw new BaseException("身份证反面不能为空");
        }
        if (ObjectUtil.isEmpty(sex)) {
            throw new BaseException("性别不能为空");
        }
        Long currentId = BaseContext.getCurrentId();
        User user = userMapper.selectById(currentId);
        //获取登录人id
        String phone = user.getPhone();
        residentConsultant.setPhone(phone);
        ResidentConsultant residentConsultant1 = residentConsultantMapper.selectPhone(phone);
        if (ObjectUtil.isNotEmpty(residentConsultant1)) {
            throw new BaseException("抱歉你已提交资料不可重复提交");
        }
        residentConsultant.setStatus(0);
        residentConsultant.setCreateTime(LocalDateTime.now());
        int insert = residentConsultantMapper.insert(residentConsultant);
        if (insert > 0) {
            User user1 = new User();
            //默认未入驻
            user1.setSettledStatus(0);
            user1.setId(currentId);
            userMapper.updateById(user1);
            return "申请成功，等待审核";
        }

        return "申请失败，请检查资料";
    }

    @Override
    public Boolean updates(ResidentConsultant residentConsultant) {
        Long id = residentConsultant.getId();
        if (ObjectUtil.isEmpty(id)) {
            throw new BaseException("请选择要修改的数据");
        }
        ResidentConsultant residentConsultant1 = residentConsultantMapper.selectById(id);
        if (ObjectUtil.isNotEmpty(residentConsultant1)) {
            Integer status = residentConsultant1.getStatus();
            if (status == 1) {
                throw new BaseException("已通过审核不可修改资料,如需修改请联系管理员");
            }
            if(status==2){
                //未通过继续修改资料等待审核
                residentConsultant.setStatus(0);
                Boolean aBoolean = residentConsultantMapper.updatePhone(residentConsultant);
                if(aBoolean==true){
                    Long currentId = BaseContext.getCurrentId();
                    User user1 = new User();
                    user1.setSettledStatus(0);
                    user1.setId(currentId);
                    userMapper.updateById(user1);
                }

                return aBoolean;
            }
        }
        Boolean aBoolean = residentConsultantMapper.updatePhone(residentConsultant);

        return aBoolean;
    }

    @Override
    public int delect(ResidentConsultantDTO residentConsultantDTO) {
        Long id = residentConsultantDTO.getId();
        int i = residentConsultantMapper.delect(id);
        return i;
    }

    @Override
    public ResidentConsultantVO selectId(ResidentConsultantDTO residentConsultantDTO) {
        String phone = residentConsultantDTO.getPhone();
        ResidentConsultant residentConsultant = residentConsultantMapper.selectPhone(phone);
        if (ObjectUtil.isEmpty(residentConsultant)) {
            throw new BaseException("抱歉你还未申请过");
        }
        ResidentConsultantVO residentConsultantVO = new ResidentConsultantVO();
        BeanUtils.copyProperties(residentConsultant, residentConsultantVO);
        if (ObjectUtil.isNotEmpty(residentConsultant.getFront())) {
            File file = fileService.getById(residentConsultant.getFront());
            String f = QiNiuConfig.CNAME + "/" + file.getFileKey();
            residentConsultantVO.setF(f);
        }
        if (ObjectUtil.isNotEmpty(residentConsultant.getReverseSide())) {
            File file = fileService.getById(residentConsultant.getReverseSide());
            String r = QiNiuConfig.CNAME + "/" + file.getFileKey();
            residentConsultantVO.setR(r);
        }
        if (ObjectUtil.isNotEmpty(residentConsultant.getAvatar())) {
            File file = fileService.getById(residentConsultant.getAvatar());
            String a = QiNiuConfig.CNAME + "/" + file.getFileKey();
            residentConsultantVO.setA(a);
        }

        if (ObjectUtil.isNotEmpty(residentConsultant.getCertificate())) {
            File file = fileService.getById(residentConsultant.getCertificate());
            String c = QiNiuConfig.CNAME + "/" + file.getFileKey();
            residentConsultantVO.setC(c);
        }

        return residentConsultantVO;
    }
}
